include(CMakePackageConfigHelpers)
# NAME 库名
# PREFIX 库前缀
# VERSION 库版本
# SOURCES 库源码 cpp
# INCLUDES 库头文件
# LINK_LIBS 库链接库
function(build_library)
    set(options       )
    set(oneValueArgs PREFIX)
    set(multiValueArgs NAME VERSION SOURCES INCLUDES LINK_LIBS)  # ← 新增 LINK_LIBS
    cmake_parse_arguments(ARGV "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

    file(GLOB_RECURSE SRC_LIST ${ARGV_SOURCES}/*.cpp ${ARGV_INCLUDES}/*.h)

    # ----------------------------
    # 构建共享库
    # ----------------------------
    if(BUILD_SHARED_LIBS)
        add_library(${ARGV_PREFIX}-${ARGV_NAME}-shared SHARED ${SRC_LIST})
        target_include_directories(${ARGV_PREFIX}-${ARGV_NAME}-shared PRIVATE ${ARGV_INCLUDES})
        set_target_properties(${ARGV_PREFIX}-${ARGV_NAME}-shared PROPERTIES
            VERSION ${ARGV_VERSION}
            SOVERSION 1
            OUTPUT_NAME ${ARGV_NAME}
            INTERFACE_INCLUDE_DIRECTORIES "$<BUILD_INTERFACE:${ARGV_INCLUDES}>;$<INSTALL_INTERFACE:include>"
        )
        set_property(TARGET ${ARGV_PREFIX}-${ARGV_NAME}-shared PROPERTY POSITION_INDEPENDENT_CODE ON)
        # ✅ 正确处理 LINK_LIBS
           if(ARGV_LINK_LIBS)
               foreach(lib IN LISTS ARGV_LINK_LIBS)
                   # 连接库
                   target_link_libraries(${ARGV_PREFIX}-${ARGV_NAME}-shared PRIVATE ${lib})
               endforeach()
           endif()
        # 安装共享库
        install(TARGETS ${ARGV_PREFIX}-${ARGV_NAME}-shared
            COMPONENT Development
            LIBRARY DESTINATION lib
            INCLUDES DESTINATION include
        )


    endif()

    # ----------------------------
    # 构建静态库
    # ----------------------------
    if(BUILD_STATIC_LIBS)
        # 构建静态库
        add_library(${ARGV_PREFIX}-${ARGV_NAME}-static STATIC ${SRC_LIST})
        # 静态库设置
        target_include_directories(${ARGV_PREFIX}-${ARGV_NAME}-static PRIVATE ${ARGV_INCLUDES})
        set_target_properties(${ARGV_PREFIX}-${ARGV_NAME}-static PROPERTIES
            OUTPUT_NAME ${ARGV_NAME}
            INTERFACE_INCLUDE_DIRECTORIES "$<BUILD_INTERFACE:${ARGV_INCLUDES}>;$<INSTALL_INTERFACE:include>"
        )
        # 静态库设置
        set_property(TARGET ${ARGV_PREFIX}-${ARGV_NAME}-static PROPERTY POSITION_INDEPENDENT_CODE ON)
        # ✅ 正确处理 LINK_LIBS
           if(ARGV_LINK_LIBS)
               foreach(lib IN LISTS ARGV_LINK_LIBS)
                   # 连接库
                   target_link_libraries(${ARGV_PREFIX}-${ARGV_NAME}-static PRIVATE ${lib})
               endforeach()
           endif()
        # 安装静态库
        install(TARGETS ${ARGV_PREFIX}-${ARGV_NAME}-static
            ARCHIVE DESTINATION lib
            COMPONENT Development
        )

    endif()
    install(DIRECTORY ${ARGV_INCLUDES}/
        DESTINATION include
        COMPONENT Development
        FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp"
    )
    # 优先使用 shared，如果没有则 fallback 到 static
    if(BUILD_SHARED_LIBS)
        add_library(${ARGV_PREFIX}::${ARGV_NAME} ALIAS ${ARGV_PREFIX}-${ARGV_NAME}-shared)
    elseif(BUILD_STATIC_LIBS)
        add_library(${ARGV_PREFIX}::${ARGV_NAME} ALIAS ${ARGV_PREFIX}-${ARGV_NAME}-static)
    endif()
    # ----------------------------
    # 生成 Config 文件
    # ----------------------------
    set(IN_CONFIG_PATH "${CMAKE_CURRENT_BINARY_DIR}/cmake/${ARGV_PREFIX}-config.cmake.in")
    file(WRITE "${IN_CONFIG_PATH}"
        "@PACKAGE_INIT@\n"
        "include(\"\${CMAKE_CURRENT_LIST_DIR}/${ARGV_PREFIX}ConfigVersion.cmake\")\n"
        "check_required_components(${ARGV_PREFIX})\n"
    )
    configure_package_config_file(
        "${IN_CONFIG_PATH}"
        "${CMAKE_CURRENT_BINARY_DIR}/cmake/${ARGV_PREFIX}-config.cmake"
        INSTALL_DESTINATION lib/cmake/${ARGV_PREFIX}
        PATH_VARS CMAKE_CURRENT_BINARY_DIR
    )

    write_basic_package_version_file(
        "${CMAKE_CURRENT_BINARY_DIR}/cmake/${ARGV_PREFIX}ConfigVersion.cmake"
        VERSION ${ARGV_VERSION}
        COMPATIBILITY SameMajorVersion
    )

    install(FILES
        "${CMAKE_CURRENT_BINARY_DIR}/cmake/${ARGV_PREFIX}-config.cmake"
        "${CMAKE_CURRENT_BINARY_DIR}/cmake/${ARGV_PREFIX}ConfigVersion.cmake"
        DESTINATION lib/cmake/${ARGV_PREFIX}
    )

endfunction()








